package com.xuan.pmh.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.xuan.pmh.R;
import com.xuan.pmh.bean.Situation;
import com.xuan.pmh.util.FinalValue;
import com.xuan.pmh.util.ToastUtil;

public class StudentSituationActivity extends HttpActivity {

	private Button btNext, btReturn;
	private TextView tvCount, tvCountMale, tvCountFemale, tvAVG, tvAVGMale,
			tvAVGFemale, tvGood, tvMiddle, tvNotPass;
	private List<Situation> situations = new ArrayList<Situation>();
	private int show = 0;

	@Override
	public void afterPost(String result, int requestCode,
			Map<String, String> request) {
		// TODO Auto-generated method stub
		gsonBean = getGsonBean(result);
		situations = gson.fromJson(gsonBean.bean,
				new TypeToken<List<Situation>>() {
				}.getType());
		if (gsonBean.state == FinalValue.SUCCESS) {
			if (situations.size() > show) {
				showSituation(situations.get(show));
			} else {
				ToastUtil.show(this, "没有问卷");
			}
		} else {

		}
	}

	public void showSituation(Situation s) {
		tvCount.setText(s.count + "人");
		tvCountMale.setText(s.countMale + "人");
		tvCountFemale.setText(s.countFemale + "人");
		tvAVG.setText(s.AVG + "分");
		tvAVGMale.setText(s.AVGMale + "分");
		tvAVGFemale.setText(s.AVGFemale + "分");
		tvGood.setText(s.good + "人");
		tvMiddle.setText(s.middle + "人");
		tvNotPass.setText(s.not_pass + "人");
	}

	@Override
	public int getLayoutId() {
		// TODO Auto-generated method stub
		return R.layout.activity_situation_student;
	}

	@Override
	public void setUI() {
		// TODO Auto-generated method stub
		btNext = (Button) mFindViewAndSetOnClick(R.id.btNext);
		btReturn = (Button) mFindViewAndSetOnClick(R.id.btReturn);
		tvCount = (TextView) findViewById(R.id.tvCount);
		tvCountMale = (TextView) findViewById(R.id.tvCountMale);
		tvCountFemale = (TextView) findViewById(R.id.tvCountFemale);
		tvAVG = (TextView) findViewById(R.id.tvAVG);
		tvAVGMale = (TextView) findViewById(R.id.tvAVGMale);
		tvAVGFemale = (TextView) findViewById(R.id.tvAVGFemale);
		tvGood = (TextView) findViewById(R.id.tvGood);
		tvMiddle = (TextView) findViewById(R.id.tvMiddle);
		tvNotPass = (TextView) findViewById(R.id.tvNotPass);

		paramsList.clear();
		addPostRequest("getSituation", paramsList, 0);
		sendPostRequest();

	}

	@Override
	public void OnClickListener(View view) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
		case R.id.btNext:
			show++;
			if (situations.size() > show) {
				showSituation(situations.get(show));
			} else {
				ToastUtil.show(this, "没有更多问卷");
				show--;
			}
			break;
		case R.id.btReturn:
			if(show==0){
				finish();
				return;
			}
			
			show--;
			if (situations.size() > show) {
				showSituation(situations.get(show));
			} else {
				ToastUtil.show(this, "不能进行此操作");
				show++;
			}
			break;

		}
	}
}
